// miniprogram/pages/publish/publish.js
const app = getApp();
var util = require('../../utils/util.js')
const db = wx.cloud.database().collection('community')
Page({
  data: {
    openid:'',
    array: [],
    photo_src:'',
    fileID:'',
    userInfo: {},
    hasUserInfo: false,
    canIUseGetUserProfile:false,
    type:"",
    recycle:{
      obj_name:'',
      obj_num:'',
      obj_price:'',
      obj_address:'',
      obj_contact:'',
      obj_note:''
    },
    partjob:{
      unit:"",
      name:'',
      time:'',
      address:'',
      content:"",
      require:'',
      salary:'',
      sign_way:'',
      note:''
    },
    college:'',
  },
  bindPickerChange: function(e) {
    this.setData({
      index: e.detail.value
    })
  },
  upload:function(e){
    const that=this;
    wx.chooseImage({
      count: 1,
      success(res){
        console.log(res.tempFilePaths[0])
        that.setData({
          photo_src:res.tempFilePaths[0]
        })
        wx.showLoading({
          title: '上传中...',
        })
        var _time = util.formatTime(new Date())
        wx.cloud.uploadFile({
          cloudPath:'gzy/'+_time+that.data.openid,
          filePath:res.tempFilePaths[0],
          success(res){
            console.log(res)
            that.setData({
              fileID:res.fileID
            })
            wx.hideLoading()
            wx.showToast({
              title: '上传成功!',
            })
            }
        })
      }
    })
  },
  formSubmit(e){
    const that=this
    
    if(!e.detail.value.type){
      wx.showToast({
        title: '请选择标题',
        icon: 'none',
      })
      return
    }
    //判断类型
    if(that.data.type=='recycle'){
      
      if(!e.detail.value.obj_name||!e.detail.value.obj_num||!e.detail.value.obj_price
        ||!e.detail.value.obj_address||!e.detail.value.obj_contact){
        wx.showToast({
          title: '请填写完整信息！',
          icon: 'none',
        })
        return
      }else{
        wx.showModal({
          title: '提示',
          content: '确认发布吗？',
          success (res) {
            if (res.confirm) {
              console.log('用户点击确定')
              console.log(that.data.userInfo)
              var _time = util.formatTime(new Date())
              db.add({
                data:{
                  username:that.data.userInfo.nickName,
                  headcut:that.data.userInfo.avatarUrl,
                  type:'二手闲置',
                  z_type:that.data.array[e.detail.value.type],
                  img:that.data.fileID,
                  recycle:{
                    obj_name:e.detail.value.obj_name,
                    obj_num:e.detail.value.obj_num,
                    obj_price:e.detail.value.obj_price,
                    obj_address:e.detail.value.obj_address,
                    obj_contact:e.detail.value.obj_contact,
                    obj_note:e.detail.value.obj_note
                  },
                  date:_time,
                  comment:0,
                  like:0,
                  collect_num:0
                }
              }).then(res => {
                wx.showToast({
                  title: '发布成功',
                })
                that.setData({
                  recycle:{
                    obj_name:'',
                    obj_num:'',
                    obj_price:'',
                    obj_address:'',
                    obj_contact:'',
                    obj_note:''
                  },
                })
                setTimeout(function () {
                  wx.navigateBack({
                    delta: 1,
                  })
                }, 1000)
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    }else if(that.data.type=='partjob'){

      if(!e.detail.value.unit||!e.detail.value.name||!e.detail.value.time||!e.detail.value.address
        ||!e.detail.value.content||!e.detail.value.require||!e.detail.value.salary||!e.detail.value.sign_way){
          wx.showToast({
            title: '请填写完整信息！',
            icon: 'none',
          })
          return
        }else{
          wx.showModal({
            title: '提示',
            content: '确认发布吗？',
            success (res) {
              if (res.confirm) {
                console.log('用户点击确定')
                console.log(that.data.userInfo)
                var _time = util.formatTime(new Date())
                db.add({
                  data:{
                    username:that.data.userInfo.nickName,
                    headcut:that.data.userInfo.avatarUrl,
                    type:'兼职实习',
                    z_type:that.data.array[e.detail.value.type],
                    img:that.data.fileID,
                    partjob:{
                      unit:e.detail.value.unit,
                      name:e.detail.value.name,
                      time:e.detail.value.time,
                      address:e.detail.value.address,
                      content:e.detail.value.content,
                      require:e.detail.value.require,
                      salary:e.detail.value.salary,
                      sign_way:e.detail.value.sign_way,
                      note:e.detail.value.note
                    },
                    date:_time,
                    comment:0,
                    like:0,
                    collect_num:0
                  }
                }).then(res => {
                  wx.showToast({
                    title: '发布成功',
                  })
                  that.setData({
                    partjob:{
                      unit:"",
                      name:'',
                      time:'',
                      address:'',
                      content:"",
                      require:'',
                      salary:'',
                      sign_way:'',
                      note:''
                    },
                  })
                  setTimeout(function () {
                    wx.navigateBack({
                      delta: 1,
                    })
                  }, 1000)
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }
    }else if(that.data.type=='college'){
      if(!e.detail.value.college){
        wx.showToast({
          title: '请填写完整信息！',
          icon: 'none',
        })
        return
      }else{
        console.log(e)
        wx.showModal({
          title: '提示',
          content: '确认发布吗？',
          success (res) {
            if (res.confirm) {
              console.log('用户点击确定')
              console.log(that.data.userInfo)
              var _time = util.formatTime(new Date())
              db.add({
                data:{
                  username:that.data.userInfo.nickName,
                  headcut:that.data.userInfo.avatarUrl,
                  type:'校园资讯',
                  z_type:that.data.array[e.detail.value.type],
                  img:that.data.fileID,
                  college:e.detail.value.college,
                  date:_time,
                  comment:0,
                  like:0,
                  collect_num:0
                }
              }).then(res => {
                wx.showToast({
                  title: '发布成功',
                })
                that.setData({
                  college:''
                })
                setTimeout(function () {
                  wx.navigateBack({
                    delta: 1,
                  })
                }, 1000)
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that=this
    that.setData({
      type:options.mode
    })
    app.getOpenId.then(res => {
      that.setData({
        openid:res
      })
    })
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that=this
    if(that.data.type=="recycle"){
      that.setData({
        array:['出闲置','收闲置']
      })
    }else if(that.data.type=="partjob"){
      that.setData({
        array:['兼职招人','实习招人']
      })
    }else if(that.data.type=="college"){
      that.setData({
        array:['校园活动','校园产品','校园生活']
      })
    }
  },


})